package com.wen.design.right;

import com.wen.design.entity.enums.PayTypeEnum;
import com.wen.design.service.PayService;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Component
public class PayFactory {
    private static final Map<PayTypeEnum, PayService> CACHE;
    static {
        CACHE = Arrays.stream(PayTypeEnum.values())
                .collect(Collectors.toMap(
                        Function.identity(),
                        PayTypeEnum::createInstance,
                        (v1,v2)->v2
                ));
    }
    public static PayService getPayService(PayTypeEnum payTypeEnum) {
        if (!CACHE.containsKey(payTypeEnum)) {
            throw new IllegalArgumentException("找不到对应类型: " + payTypeEnum.getType());
        }
        return CACHE.get(payTypeEnum);
    }

    public static PayService getPayService(String type) {
        PayTypeEnum payTypeEnum = PayTypeEnum.getPayType(type);
        return getPayService(payTypeEnum);
    }
}
